package com.mnm.seekbarpreference;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.SeekBar;
import android.widget.TextView;

import com.hust.iamk.R;

public class NumberPickerDialogPreference extends DialogPreference {
	// Namespaces to read attributes
	private static final String PREFERENCE_NS = "http://schemas.android.com/apk/res-auto";
	private static final String ANDROID_NS = "http://schemas.android.com/apk/res/android";

	// Default values for defaults
	private static final int DEFAULT_CURRENT_VALUE = 50;
	private static final int DEFAULT_MIN_VALUE = 0;
	private static final int DEFAULT_MAX_VALUE = 100;

	private final int mMinValueLeft;
	private final int mMaxValueLeft;
	private final int mDefaultLeft;
	private final int mMinValueRight;
	private final int mMaxValueRight;
	private final int mDefaultRight;

	private int mValueLeft;
	private int mValueRight;

	private NumberPicker mNumberPickerLeft;
	private NumberPicker mNumberPickerRight;
	private String titleLeft;
	private String titleRight;

	public NumberPickerDialogPreference(Context context, AttributeSet attrs) {
		super(context, attrs);

		// get attributes specified in XML
		mMinValueLeft = attrs.getAttributeIntValue(PREFERENCE_NS, "minLeft",
				DEFAULT_MIN_VALUE);
		mMaxValueLeft = attrs.getAttributeIntValue(PREFERENCE_NS, "maxLeft",
				DEFAULT_MAX_VALUE);
		mDefaultLeft = attrs.getAttributeIntValue(ANDROID_NS, "defaultLeft",
				DEFAULT_CURRENT_VALUE);

		mMinValueRight = attrs.getAttributeIntValue(PREFERENCE_NS, "minRight",
				DEFAULT_MIN_VALUE);
		mMaxValueRight = attrs.getAttributeIntValue(PREFERENCE_NS, "maxRight",
				DEFAULT_MAX_VALUE);
		mDefaultRight = attrs.getAttributeIntValue(ANDROID_NS, "defaultRight",
				DEFAULT_CURRENT_VALUE);

		titleLeft = attrs.getAttributeValue(PREFERENCE_NS, "titleLeft");
		titleRight = attrs.getAttributeValue(PREFERENCE_NS, "titleRight");

		// // set layout
		// setDialogLayoutResource(R.layout.preference_number_picker_dialog);
		// setPositiveButtonText(android.R.string.ok);
		// setNegativeButtonText(android.R.string.cancel);
		// setDialogIcon(null);
	}

	@Override
	protected View onCreateDialogView() {
		// TODO Auto-generated method stub
		// Get current value from preferences
		// mValueLeft = getPersistedInt(mDefaultLeft);
		mValueLeft = Integer.parseInt(getPersistedString(
				Integer.toString(mDefaultLeft) + "_"
						+ Integer.toString(mDefaultLeft)).split("_")[0]);
		mValueRight = Integer.parseInt(getPersistedString(
				Integer.toString(mDefaultRight) + "_"
						+ Integer.toString(mDefaultRight)).split("_")[1]);

		// Inflate layout
		LayoutInflater inflater = (LayoutInflater) getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.preference_number_picker_dialog,
				null);

		TextView dialogMessageText = (TextView) view
				.findViewById(R.id.text_dialog_message_1);
		dialogMessageText.setText(getDialogMessage());
		TextView txtLeft = (TextView) view
				.findViewById(R.id.text_dialog_message_1);
		TextView txtRight = (TextView) view
				.findViewById(R.id.text_dialog_message_2);

		txtLeft.setText(titleLeft);
		txtRight.setText(titleRight);

		mNumberPickerLeft = (NumberPicker) view
				.findViewById(R.id.number_picker_1);
		mNumberPickerRight = (NumberPicker) view
				.findViewById(R.id.number_picker_2);
		mNumberPickerLeft.setMinValue(mMinValueLeft);
		mNumberPickerLeft.setMaxValue(mMaxValueLeft);
		mNumberPickerLeft.setValue(mValueLeft);

		mNumberPickerRight.setMinValue(mMinValueRight);
		mNumberPickerRight.setMaxValue(mMaxValueRight);
		mNumberPickerRight.setValue(mValueRight);

		return view;

	}

	// public void setValue(int value) {
	// value = Math.max(Math.min(value, mMaxValue), mMinValue);
	//
	// if (value != mValue) {
	// mValue = value;
	// persistInt(value);
	// notifyChanged();
	// }
	// }

	@Override
	protected void onDialogClosed(boolean positiveResult) {
		super.onDialogClosed(positiveResult);

		// when the user selects "OK", persist the new value
		if (positiveResult) {
			int numberPickerValueLeft = mNumberPickerLeft.getValue();
			int numberPickerValueRight = mNumberPickerRight.getValue();
			numberPickerValueLeft = numberPickerValueLeft > mMaxValueLeft ? mMaxValueLeft
					: numberPickerValueLeft;
			numberPickerValueRight = numberPickerValueRight > mMaxValueLeft ? mMaxValueLeft
					: numberPickerValueRight;
			persistString(Integer.toString(numberPickerValueLeft) + "_"
					+ Integer.toString(numberPickerValueRight));

		}
	}
	//
	// @Override
	// protected Parcelable onSaveInstanceState() {
	// // save the instance state so that it will survive screen orientation
	// // changes and other events that may temporarily destroy it
	// final Parcelable superState = super.onSaveInstanceState();
	//
	// // set the state's value with the class member that holds current
	// // setting value
	// final SavedState myState = new SavedState(superState);
	// myState.minValue = getMinValue();
	// myState.maxValue = getMaxValue();
	// myState.value = getValue();
	//
	// return myState;
	// }

	// @Override
	// protected void onRestoreInstanceState(Parcelable state) {
	// // check whether we saved the state in onSaveInstanceState()
	// if (state == null || !state.getClass().equals(SavedState.class)) {
	// // didn't save the state, so call superclass
	// super.onRestoreInstanceState(state);
	// return;
	// }
	//
	// // restore the state
	// SavedState myState = (SavedState) state;
	// setMinValue(myState.minValue);
	// setMaxValue(myState.maxValue);
	// setValue(myState.value);
	//
	// super.onRestoreInstanceState(myState.getSuperState());
	// }

	// private static class SavedState extends BaseSavedState {
	// int minValue;
	// int maxValue;
	// int value;
	//
	// public SavedState(Parcelable superState) {
	// super(superState);
	// }
	//
	// public SavedState(Parcel source) {
	// super(source);
	//
	// minValue = source.readInt();
	// maxValue = source.readInt();
	// value = source.readInt();
	// }
	//
	// @Override
	// public void writeToParcel(Parcel dest, int flags) {
	// super.writeToParcel(dest, flags);
	//
	// dest.writeInt(minValue);
	// dest.writeInt(maxValue);
	// dest.writeInt(value);
	// }
	//
	// @SuppressWarnings("unused")
	// public static final Parcelable.Creator<SavedState> CREATOR = new
	// Parcelable.Creator<SavedState>() {
	// @Override
	// public SavedState createFromParcel(Parcel in) {
	// return new SavedState(in);
	// }
	//
	// @Override
	// public SavedState[] newArray(int size) {
	// return new SavedState[size];
	// }
	// };
	// }
}